package com.home.mq;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;


@Component
public class MqProducer {


    @Resource
    private RocketMQTemplate mqTemplate;

    // 消息发送方法
    public SendResult sendMessage(String topic, String msg){
        for(int i=0;i<5;i++) {
            TransactionSendResult result = mqTemplate.sendMessageInTransaction(
                    topic, MessageBuilder.withPayload(msg+":::" + i).build(), i);
            System.out.println(result);

        }

        //SendResult sendResult=mqTemplate.syncSend(topic,message);
                //mqTemplate.sendMessageInTransaction(topic,message,null);
        return null;
    }


}
